package notifier

import (
	"bytes"
	"encoding/json"
	"feishu-alter/global"
	"feishu-alter/model"
	"feishu-alter/transformer"
	"log"
	"net/http"
)

func Send(notification model.Notification) error {
	data, err := transformer.TransformToFeishu(notification)
	if err != nil {
		return err
	}
	body, err := json.Marshal(data)
	if err != nil {
		return err

	}
	client := &http.Client{}
	req, err := http.NewRequest("POST", global.Config.WebhookUrl, bytes.NewBuffer(body))
	req.Header.Set("Content-Type", "application/json")
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	log.Printf("告警发送状态：%s\n", resp.Status)
	return nil
}
